#include <iostream>

using namespace std;

class Test
{
	static const int COUNT = 4;
	static char c_buffer[];
	static char c_map[];
public:
	void* operator new (size_t size)
	{
		void* ret = NULL;
		for (int i = 0; i < COUNT; i++)
		{
			if (!c_map[i])
			{
				c_map[i] = 1;
				ret = c_buffer + i * sizeof(Test);
				cout << "Success to alloc memory with : " << ret << endl;
				break;
			}
		}
		return ret;
	}

	void operator delete (void* p)
	{
		if (p != NULL)
		{
			char* map = reinterpret_cast<char*>(p);
			size_t index = (map - c_buffer) / sizeof(Test);
			size_t flag = (map - c_buffer) % sizeof(Test);
			if ((0 <= index && COUNT > index) && (flag == 0))
			{
				c_map[index] = 0;
				cout << "Successed to delete memory with : " << p << endl;
			}
		}
	}

	void operator delete[](void* p)
	{
		if (p != NULL)
		{
			for (int i = 0; i < COUNT; i++)
			{
				if (c_map[i])
				{
					c_map[i] = 0;
					cout << "Successed to delete memory with : " << reinterpret_cast<void*>(c_buffer + i * sizeof(Test)) << endl;
				}
			}
		}
	}
};

char Test::c_buffer[sizeof(Test) * Test::COUNT] = { 0 };
char Test::c_map[Test::COUNT] = { 0 };

int main(int argc, char **argv)
{
	Test *t1 = new Test;
	delete t1;
	cout << "--------------------------------" << endl;
	Test* tbuf[5] = { 0 };
	for (int i = 0; i < 5; i++)
	{
		tbuf[i] = new Test();
		cout << "tbuf[" << i << "] = " << tbuf[i] << endl;
	}

	cout << "--------------------------------" << endl;
	delete[] tbuf[0];
	/*for (int i = 0; i < 5; i++)
	{
		cout << "delete " << tbuf[i] << endl;
		delete tbuf[i];
	}*/
	return 0;
}